import * as module from "@/tools/util";
import * as Request from '@/api/liubike';
import store from "@/tools/factory"
import { config, isDev } from '@/api/config';

import wx  from '@/tools/wx'
import commonJs from '@/views/commonJs/common'
let that
let loginInfo = null;

/**
 * 转化:   this.data -> this._data
 * ev.detail -> ev.target
 * options -> this.$route.query
 */

export default {
    mixins: [commonJs],
    data() {
        return {
            title: "加载中...", // 状态
            cashList: [],
            discountList: [],
            appData: {},
            type: "", // 数据类型
            loading: true, // 显示等待框
            disabled: false,
            status: false,
            code: ""
        }
    },
    mounted() {
        that = this;
    },
    methods: {
        onShareAppMessage: function () {
            return {
                title: '小遛共享',
                path: '/index',
            }
        },
        onLoad(options) {

        },
        getCode(ev) { // 获取用户输入手机号码
            that.setData({ code: ev.target.value });
        },
        exchange() {
            if (!that._data.code) {
                wx.showModal({
                    title: "温馨提示",
                    content: "请输入兑换码",
                    showCancel: false
                });
                return false
            }
            that.setData({
                disabled: true
            })
            wx.showLoading({
                title: "兑换中..."
            });
            Request.couponCode({ code: that._data.code }).then(res => {
                wx.hideLoading();
                that.setData({
                    disabled: false
                })
                if (res.success) {
                    if (res.data) {
                        let cashList = [];
                        let discountList = [];
                        for (let i = 0; i < res.data.cashCouponQty; i++) {
                            cashList.push(i)
                        }
                        for (let i = 0; i < res.data.discountCouponQty; i++) {
                            discountList.push(i)
                        }
                        res.data.giveBalance = res.data.giveBalance.toFixed(2);
                        res.data.cashCouponAmt = res.data.cashCouponAmt.toFixed(2);
                        that.setData({
                            status: true,
                            appData: res.data,
                            cashList: cashList,
                            discountList: discountList
                        })
                    }
                } else {
                    wx.showModal({
                        title: "兑换失败",
                        content: res.msg,
                        showCancel: false
                    });
                }
            })
        }
    }
}
